<?php

namespace app\admin\controller\ykjp\purchase;

use app\common\controller\Backend;

/**
 * 采购审批
 *
 * @icon fa fa-circle-o
 */
class Approval extends Backend {

    protected $model = null;
    protected $distinguish = true;
    protected $wherename = "";

    public function _initialize() {
        parent::_initialize();
        $this->model = new \app\admin\model\ykjp\purchase\Order;
        $this->view->assign("typeList", $this->model->getTypeList());
        $this->view->assign("statusList", $this->model->getStatusList());
    }

    /**
     * 审批列表
     */
    public function index() {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            $this->wherename = "order.firmid";
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                    ->with(['supplier'])
                    ->where($where)
                    ->order($sort, $order)
                    ->count();

            $list = $this->model
                    ->with(['supplier'])
                    ->where($where)
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();

            foreach ($list as $row) {

                $row->getRelation('supplier')->visible(['name']);
            }
            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 编辑
     */
    public function edit($ids = null) {
        list($where, $sort, $order, $offset, $limit) = $this->buildparams();
        $modelHouse = new \app\admin\model\ykjp\information\basisinfo\Warehouse;
        $modelPaty = new \app\admin\model\ykjp\information\basisinfo\Partition;
        $detail = $this->model
                ->where($where)
                ->order($sort, $order)
                ->where("id", $ids)
                ->select();
        if ($detail) {
            $warehouse_id = $detail[0]['warehouse_id'];
            $hso = $modelHouse->where("id", $warehouse_id)->field("name")->find();
            $detail[0]['warehouse_id'] = $hso['name'];
            $pat = $modelPaty->where("id", $detail[0]['warehouse_partition_id'])->field("name")->find();
            $detail[0]['warehouse_partition_id'] = $pat['name'];
        }
        $productModel = new \app\admin\model\ykjp\purchase\Product;
        $product = $productModel->with(['products'])->where("purchase_id", $ids)->select();
        $arr;
        if ($product) {
            foreach ($product as $key => $value) {
                $a = json_decode($value['products']['prop']);
                $value['products']['prop'] = $this->model->object_array($a);
            }
        }
        $storageMode = new \app\admin\model\ykjp\purchase\Storage;
        $isCount = $storageMode->where("firmid", $this->auth->firmid)->where("purchase_id", $ids)->count();
        $this->assign("isCount", $isCount);
        $this->assign("detail", $detail);
        $this->assign("product", $product);
        return $this->view->fetch();
    }

    /**
     * 审批
     */
    public function updateSatus($ids = null) {
        if ($this->request->isAjax()) {
            $modelProduct = new \app\admin\model\ykjp\purchase\Product;
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            if ($this->request->param("type") == "yes") {
                $data = array("status" => 2, "purman_id" => $this->auth->id, "updatetime" => time());
                $update = $this->model
                        ->where("status", 0)
                        ->where("id", "in", $ids)
                        ->update($data);
                if ($update) {
                    $proData = array("status" => 2, "updatetime" => time());
                    $a = $modelProduct->where("purchase_id", "in", $ids)->update($proData);
                    // var_dump($a);exit;
                    $this->success(null, null, array("result" => "success"));
                }
                $this->error("operation fail");
            } else {
                $reason = $this->request->param("reason");
                if (!$reason) {
                    $this->error(__("Reason cannot be empty"));
                }
                $reason = strip_tags($reason);
                $data = array("status" => 1, "purman_id" => $this->auth->id, "updatetime" => time(), "reason" => $reason);
                $update = $this->model
                        ->where("status", 0)
                        ->where("id", "in", $ids)
                        ->update($data);

                if ($update) {
                    $proData = array("status" => 1, "updatetime" => time());
                    $modelProduct->where("purchase_id", "in", $ids)->update($proData);
                    $this->success(null, null, array("result" => "success"));
                }
                $this->error(__("operation fail"));
            }
        }
        $this->error(__("Illegal operation"));
    }

    /**
     * 搜索审批人
     */
    public function getsalesman() {
        $result = array("rows" => [], "total" => 0);
        if ($this->request->isAjax()) {
            $admin = model("admin");
            $admin = model("admin");
            $list = $admin
                    ->field("id,nickname")
                    ->select();
            $count = $admin
                    ->field("id,nickname")
                    ->count();
            $result = array("rows" => $list, "total" => $count);
            return json($result);
        }
        return json($result);
    }

}
